package com.king.run.activity.other;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.king.run.R;
import com.king.run.activity.circle.CircleFragment;
import com.king.run.activity.mine.MineFragment;
import com.king.run.activity.posture.PostureFragment;
import com.king.run.activity.sport.SportFragment;
import com.king.run.base.BaseActivity;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * 作者：shuizi_wade on 2017/8/18 16:25
 * 邮箱：674618016@qq.com
 */
@ContentView(R.layout.activity_home)
public class HomeActivity extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.iv_sport)
    ImageView iv_sport;
    @ViewInject(R.id.iv_circle)
    ImageView iv_circle;
    @ViewInject(R.id.iv_posture)
    ImageView iv_posture;
    @ViewInject(R.id.iv_mine)
    ImageView iv_mine;
    @ViewInject(R.id.tv_sport)
    TextView tv_sport;
    @ViewInject(R.id.tv_circle)
    TextView tv_circle;
    @ViewInject(R.id.tv_posture)
    TextView tv_posture;
    @ViewInject(R.id.tv_mine)
    TextView tv_mine;
    private SportFragment sportFragment;
    private CircleFragment circleFragment;
    private PostureFragment postureFragment;
    private MineFragment mineFragment;
    private int mIndex;
    private Fragment[] mFragments;
    private FragmentTransaction transaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
    }

    private void initViews() {
        transaction = getSupportFragmentManager().beginTransaction();
        sportFragment = new SportFragment();
        circleFragment = new CircleFragment();
        postureFragment = new PostureFragment();
        mineFragment = new MineFragment();
        mFragments = new Fragment[]{sportFragment, circleFragment, postureFragment, mineFragment};
        transaction.add(R.id.container, sportFragment);
        transaction.commit();
        setIndexSelected(0);
        findViewById(R.id.ly_sport).setOnClickListener(this);
        findViewById(R.id.ly_circle).setOnClickListener(this);
        findViewById(R.id.ly_posture).setOnClickListener(this);
        findViewById(R.id.ly_mine).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_sport:
                setIndexSelected(0);
                break;
            case R.id.ly_circle:
                setIndexSelected(1);
                break;
            case R.id.ly_posture:
                setIndexSelected(2);
                break;
            case R.id.ly_mine:
                setIndexSelected(3);
                break;
        }
    }
    public void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //隐藏
        ft.hide(mFragments[mIndex]);
        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.container, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }
        ft.commit();
        //再次赋值
        mIndex = index;
    }
}
